package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhuss.enums.StartStopEnum;
import com.zhuss.enums.SystemEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.diss.SearchDissBo;
import com.zhuss.bean.vo.core.diss.ListDissVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.DissDao;
import com.zhuss.core.db.entity.Diss;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;


/**
 * @description: 吐槽读
 * @author: zhushanshuo
 * @create: Tue Feb 02 15:22:08 CST 2021
 **/
@Component("dissRead")
public class DissRead {

	@Autowired
	private DissDao dissDao;


	public Map<String, Diss> listMap() {
		List<Diss> list = dissDao.listAll();
		Map<String, Diss> map = new HashMap<>();
		for (Diss c : list) {
			map.put(c.getTitle(), c);
		}
		return map;
	}

	/**
	 * 吐槽
	 * @return
	 */
	public Object listDiss() {
		List<Diss> dissList = dissDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(dissList, result);
		return result;
	}


	/**
	 * 吐槽
	 * @return
	 */
	public Object listDiss(List<Long> dissIdList) {
		List<Diss> result = dissDao.listById(dissIdList);
		return result;
	}


	/**
	 * 根据名字查询吐槽
	 * @param
	 * @return
	 */
	public List<Diss> list(String name) {
		List<Diss> result = dissDao.listByName(name);
		return result;
	}

	public List<Diss> listNotId(String name, Long dissId) {
		List<Diss> result = new ArrayList<Diss>();
		List<Diss> list = list(name);
		for (Diss c : list) {
			if (!c.getDissId().equals(dissId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}

	/**
	 * 吐槽
	 * @return
	 */
	public Object listDissId() {
		List<Diss> dissList = dissDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(dissList, result);
		return result;
	}

	private void transferListId(List<Diss> dissList, List<Long> result) {
		for (Diss item : dissList) {
			result.add(item.getDissId());
		}
	}

	private void transferList(List<Diss> dissList, List<ListVo> result) {
		for (Diss item : dissList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getDissId());
			vo.setLabel(item.getTitle());
			result.add(vo);
		}

	}

	/**
	 * 吐槽列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchDissBo bo) {
		PageVo<ListDissVo> result = new PageVo<ListDissVo>();
		Integer system = bo.getConcurrentSystem();
		if (SystemEnum.APP.sameKey(system)) {
			bo.setCreatorId(bo.getConcurrentUserId());
			bo.setStatus(StartStopEnum.启用.key());
		}
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(dissDao.list(bo));
        result.setTotal(dissDao.countList(bo));
		return result;
	}

	/**
	 * 吐槽详情
	 * @param dissId
	 * @return
	 * @throws DzcxException
	 */
	public Object detail(Long dissId) throws DzcxException {
		Object result = dissDao.detail(dissId);
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		}
		return result;
	}



}
